/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.Panel;
import com.jb.web.bean.Label;
import com.jb.web.bean.TabbedPanel;
import com.jb.web.context.WebContext;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;

public class AJaxTabbedPanelRenderer extends RegionRenderer {
    public StringBuffer renderChildren(WebContext ctx, UIComponent item) {
        TabbedPanel sect = (TabbedPanel)item;
        if ("top".equals(sect.getTabPosition()) || 
            "bottom".equals(sect.getTabPosition())) {
            return renderTB(ctx, sect);
        } else {
            return renderLR(ctx, sect);
        }
    }
    
    private boolean hideTab(WebContext ctx, TabbedPanel sect) {
        return sect.isHideTabs() && (sect.getPage().isDialogMode() ||ctx.isRunTime());
    }
    
    private StringBuffer renderLR(WebContext ctx, TabbedPanel sect) {
        StringBuffer html = new StringBuffer();
        if (!hideTab(ctx, sect) && 
            "left".equals(sect.getTabPosition())) {
            html.append(renderTabs(ctx, sect));
        }
        Panel subSect = sect.getSelected();
        if (subSect != null) {
            html.append(subSect.render(ctx));
        }
        if (!hideTab(ctx, sect) && 
            "right".equals(sect.getTabPosition())) {
            html.append(renderTabs(ctx, sect));
        }
        return html;
    }

    private StringBuffer renderTB(WebContext ctx, TabbedPanel sect) {
        StringBuffer html = new StringBuffer();
        if (!hideTab(ctx, sect)
            && "top".equals(sect.getTabPosition())
            )
            html.append(renderTabs(ctx, sect));

        Panel subSect = sect.getSelected();

        if (subSect != null) {
            html.append(subSect.render(ctx));
        }

        if (!hideTab(ctx, sect) && 
            "bottom".equals(sect.getTabPosition()))
            html.append(renderTabs(ctx, sect));
        return html;
    }

    private String renderTabs(WebContext ctx, TabbedPanel sect) {
        Label header = sect.getHeader();
        StringBuffer html = 
            new StringBuffer("<table cellpadding=\"0\" cellSpacing=\"0\"  border=\"0\" ");
        html.append(addTagProperty("width", "100%", null));
        html.append(addTagProperty("id", sect.getClientId() + "table", null));
        html.append("><tr><td style=\"width:12px;background-color:transparent;\" >&nbsp;</TD>");

        String headLabel = evaluate(ctx, header, header.getValue());

        if (isEmptyString(headLabel)) {
            headLabel = "&nbsp;&nbsp";
        }

        if ("right".equalsIgnoreCase(sect.getHeaderAlign())) {
            html.append("<TD align=\"left\" style=\"width:100%;background-color:transparent;\" ><SPAN ");
            html.append(getEditScript(ctx, header));
            html.append(addTagProperty("style", header.getStyle(ctx), 
                                       null)).append(" >");
            if (!isEmptyString(header.getIconImage())) {
                html.append("<img class=\"jbLabelImg\" ");
                html.append(addTagProperty("src", header.getIconImage(), 
                                           null));
                html.append(" />");
            }

            html.append(headLabel);
            html.append("</span></TD>");
        }

        Iterator iter = sect.getChildPanels().iterator();

        while (iter.hasNext()) {
            Panel aSect = (Panel)iter.next();

            if (aSect != null) {
                Label aTitle = aSect.getHeader();

                if (!aTitle.isVisible(ctx)) {
                    continue;
                }

                String label = evaluate(ctx, aTitle, aTitle.getValue());

                if (isEmptyString(label)) {
                    label = aSect.getId();
                }

                if (sect.isSelected(aSect)) {
                    String headStyle = aTitle.getStyle(ctx);

                    html.append("<td valign=\"bottom\"><DIV ");
                    html.append(getEditScript(ctx, aTitle));
                    html.append(addTagProperty("class", aTitle.getStyleClass(), 
                                               null));
                    html.append(addTagProperty("style", headStyle, null));
                    html.append(">");

                    if (!isEmptyString(aTitle.getIconImage())) {
                        html.append("<img  class=\"jbLabelImg\" ");
                        html.append(addTagProperty("src", 
                                                   aTitle.getIconImage(), 
                                                   null));
                        html.append(" />");
                    }

                    html.append(label);
                    html.append("</div></td>");
                } else {
                    String headStyle = aTitle.getStyle(ctx);
                    html.append("<td valign=\"bottom\"><div ");

                    //  .append(getEditScript(ctx, header))
                    html.append(addTagProperty("class", header.getStyleClass(), 
                                               null));
                    html.append(addTagProperty("style", headStyle, null));
                    html.append(" onclick=");
                    html.append("\"JB._e('");
                    html.append(ctx.getPageName() + ".xsp");
                    html.append("','").append(aSect.getClientId()).append("','");
                    html.append("com.jb.web.bean.event.OnSelectTab");
                    html.append("','");
                    html.append(sect.getClientId());
                    html.append("');\"");
                    html.append(">");

                    if (!isEmptyString(aTitle.getIconImage())) {
                        html.append("<img  class=\"jbLabelImg\" ");
                        html.append(addTagProperty("src", 
                                                   aTitle.getIconImage(), 
                                                   null));
                        html.append(" />");
                    }

                    html.append(label);
                    html.append("</DIV></TD>");
                }
            }
        }

        if (!("right".equalsIgnoreCase(sect.getHeaderAlign()))) {
            html.append("<TD align=\"right\" style=\"width:100%;background-color:transparent;\" ><SPAN ");
            html.append(getEditScript(ctx, header));
            html.append(addTagProperty("style", header.getStyle(ctx), 
                                       null)).append(" >");

            if (!isEmptyString(header.getIconImage())) {
                html.append("<img class=\"jbLabelImg\" ");
                html.append(addTagProperty("src", header.getIconImage(), 
                                           null)).append(" />");
            }

            html.append(headLabel).append("</span></td>");
        }

        html.append("<td style=\"width:12px;background-color:transparent;\" >&nbsp;</td></tr></table>");

        return html.toString();
    }
}
